<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace kitego\extend\storage\impl;

use kitego\extend\storage\Storage;
use kitego\services\common\FileService;
use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

class QiniuImpl extends Storage
{
    protected $config;
    protected $auth;

    public function __construct($config)
    {
        $this->config = $config;
        $this->auth = new Auth($this->config['qiniu_access_key'], $this->config['qiniu_secret_key']);
    }

    /**
     * 上传
     */
    public function upload($path)
    {
        $token = $this->auth->uploadToken($this->config['qiniu_bucket']);
        $uploadMgr = new UploadManager();
        list(, $error) = $uploadMgr->putFile($token, $path . '/' . $this->fileName, $this->fileInfo['realPath'], null, 'application/octet-stream', true, null, 'v2');
        if ($error !== null) {
            $this->error = $error->message();
            return false;
        }

        return true;
    }

    /**
     * 删除
     */
    public function delete($path)
    {
        $bucketManager = new BucketManager($this->auth, app()->make(Config::class));
        list(, $error) = $bucketManager->delete($this->config['qiniu_bucket'], $path);

        if ($error !== null) {
            $this->error = $error->message();
            return false;
        }

        return true;
    }

    public function water($path)
    {
        // TODO: Implement water() method.
    }
}